/**
 * @author helio
 */
var ShippingOperatePanel = Ext.extend(BasicPanel,{
	initlize : function() {
		if(!this.detailsGrid) {
						
			this.detailsStore = new Ext.data.Store({
			    proxy: new Ext.ux.data.PagingMemoryProxy(this.detailsJson),
			    //remoteSort:true,
			    reader: new Ext.data.JsonReader({
					root: 'products',
					remoteSort: true,
			        fields: ['index', 'bcode','pcode', 'pname', {name:'pnumber',type:'float'}, 'punit', 'pamount', {name:'snumber',type:'float'}, {name:'number',type:'float'}, 'storage']
			    })
			});
						
			// create the grid
			var csm = new Ext.grid.CheckboxSelectionModel();
			
			this.detailsGrid = new Ext.grid.EditorGridPanel({
				store: this.detailsStore,
				
				viewConfig: {
			        forceFit: true,
					enableRowBody:true,
		            showPreview:true
			    },
				stripeRows: true,
				border: false,
				autoExpandColumn: 'pname',
				layout: 'fit',
				
				columns: [csm, {
					header: "商品编号",
					dataIndex: 'pcode',
					width: 80,
					sortable: true
				}, {
					header: "商品名称",
					dataIndex: 'pname',
					sortable: true
				}, {
					header: "销售数量",
					dataIndex: 'pnumber',
					align: 'right',		
					width: 70,
					sortable: true
				}, {
					header: "单位",
					dataIndex: 'punit',
					width: 45,
					sortable: true
				}, {
					header: "出货仓库",
					dataIndex: 'storage',
					width: 70,
					sortable: true,
					
					editor: new Ext.form.ComboBox({
						tpl: comboxTpl,
				        store: DataStoreUtil.getStorageStore(),
				        displayField:'name',
				        typeAhead: true,
				        mode: 'local',
				        triggerAction: 'all',
				        selectOnFocus:true
					})
				}, {
					header: "已出货数",
					dataIndex: 'snumber',
					align: 'right',
					width: 70
				}, {
					header: "出货数量",
					dataIndex: 'number',
					align: 'right',		
					width: 70,
					editor: new Ext.form.NumberField({
	               		allowBlank: true
	           		})
				}],
				
				sm: csm,
				
				tbar:[{
		            text:'全部出货',
		            tooltip:'商品出货',
		            iconCls:'add',
					handler: this.shippingAllProducts.createDelegate(this)
		        }, '-', {
		            text:'选中商品出货',
		            tooltip:'商品出货',
		            iconCls:'option',
					handler: this.shippingPartProducts.createDelegate(this)
		        }]
				
			});
			
		}
	},
	
	initlizeDataHandler : function(resultJson) {
		this.detailsJson['products'] = resultJson.products;
		this.detailsStore.load({params:{start:0, limit:25}});
	},
	
	getCorePanel : function() {
		if (!this.detailsGrid) {
			this.initlize();
		}
		return this.detailsGrid;
	},
	
	shippingAllProducts : function() {
		this.confirmMessage('出货全部商品。还要继续吗？', '提示信息', function(btn){
			if (btn == "ok") {
				
				var products = [];	
				var store = this.detailsGrid.store;
				var records = store.getRange(0, store.getTotalCount());
				for(var i=0;i<records.length;i++) {
					if(records[i].data.pcode) {
						var record = records[i];		
						products[i] = record.data;
					}			
				}
				
				this.checkShippingData(products, 1);
			}
		}.createDelegate(this));		
	},
	
	shippingPartProducts : function() {
		this.selectedRecordList = this.detailsGrid.getSelectionModel().getSelections();
		if(this.selectedRecordList.length == 0) {
			this.warnMessage('请先选择一条数据！！！', '警告信息');
		} else {
			this.confirmMessage('出货选中的商品。还要继续吗？', '提示信息', function(btn){
				if (btn == "ok") {
					var products = [];
					
					for(var i=0;i<this.selectedRecordList.length;i++) {
						if(this.selectedRecordList[i].data.pcode) {
							var record = this.selectedRecordList[i];		
							products[i] = record.data;	
						}			
					}
					
					this.checkShippingData(products, 2);					
				}
			}.createDelegate(this));
		} 
	},
	
	shippingAllSuccessHandler : function() {
		this.showMessage('商品出货成功！！！');
		
		var store = this.detailsGrid.store;
		var records = store.getRange(0, store.getTotalCount());
		
		for(var i=0;i<records.length;i++) {
			if(records[i].data.pcode) {
				var record = records[i];		
				record.set("snumber", record.get("snumber") + record.get("number"));	
				record.set("number", record.get("pnumber") - record.get("snumber"));	
			}			
		}
	},
	
	shippingPartSuccessHandler : function() {
		this.showMessage('商品出货成功！！！');
		var records = this.detailsGrid.getSelectionModel().getSelections();
				
		for(var i=0;i<records.length;i++) {
			if(records[i].data.pcode) {
				var record = records[i];		
				record.set("snumber", record.get("snumber") + record.get("number"));	
				record.set("number", record.get("pnumber") - record.get("snumber"));	
			}			
		}
	},
	
	checkShippingData : function(products, type) {
		if(products.length == 0) {
			this.warnMessage('没有出货商品信息！！！', '警告信息');
		} else {
			
			for(var i = 0; i < products.length; i++ ) {
				var product = products[i];
				
			}			
			
			if (type == 1) {
				SALE.Action.shippingSaleProducts(this.shippingAllSuccessHandler.createDelegate(this), products);
			} else {
				SALE.Action.shippingSaleProducts(this.shippingPartSuccessHandler.createDelegate(this), products);
			}
		}				
	}
});